from common.utils.rights import permission_required, view_logging_required
from common.utils.file_read import file_read
from . import index_bp
from flask import render_template,request
from flask_wtf.form import CombinedMultiDict
from models.form import UploadForm
import cv2 as cv


# 用户增加
@index_bp.get('/gama_pro')
@index_bp.post('/gama_pro')
@view_logging_required
@permission_required("gama:pro")
def gama_pro():
    if request.method == "GET":
        return render_template('admin/cv/gama.html')
    else:
        form = UploadForm(CombinedMultiDict([request.form, request.files]))
        if form.validate():
            f = request.files['file']
            gamma = float(request.form.get('gamma'))
            file_url = file_read(f)
            y = gamma
            y2 = 1 - y
            picData = cv.imread(file_url,0)
            w = picData.shape[0]
            h = picData.shape[1]
            for i in range(w):
                for j in range(h):
                    picData[i, j] = 255 ** (y2) * picData[i, j] ** y
            res_pic = "static/download/gama.jpg"
            cv.imwrite(res_pic, picData)
            return render_template('admin/cv/gama.html',pic=file_url,pic_res=res_pic)
        else:
            return "请传入正确的文件格式"